import requests
import pytest
import json
import pymysql
# def readjson():
#     with open(file='data.json',mode='r',encoding='utf-8') as f:
#         data=json.load(f)
#         return data


class TestZf:
    token=None
    gid=None
    sid=None
    def setup_class(self):
        print("用例初始化-----")
    def teardown_class(self):
        print("执行结束")
    #登录
    def test_dl(self):
        try:
            url = "http://10.59.9.12:7001/api/public/login"
            data = {"username": "13811033962", "password": "123456"}
            headers = {"content-type": "application/json"}
            res = requests.post(url=url, headers=headers, json=data)
        except:
            print("登录接口连接失败")
        print("登录成功:",res.json())
        TestZf.token ="Bearer "+res.json()['data']['token']
        assert res.status_code==200
        assert "ok" in res.text
    def test_xq(self):
        url="http://10.59.9.12:7001/api/goods/view?id=32"
        headers ={"auth-token":TestZf.token}
        res=requests.get(url=url,headers=headers)
        print("商品详情:", res.json())
        TestZf.sid=res.json()['data']['goods']['id']
        print(TestZf.sid)
        assert res.status_code == 200
        assert "ok" in res.text
    def test_jrgwc(self):
        url="http://10.59.9.12:7001/api/cart/add"
        headers = {"content-type":"application/json","auth-token": TestZf.token}
        data={"id":TestZf.sid,"sku_id":"","num":1}
        res = requests.post(url=url, headers=headers, json=data)
        print("加入购物车:", res.json())
        assert res.status_code == 200
        assert "成功" in res.text
    def test_lb(self):
        url="http://10.59.9.12:7001/api/cart?page=1"
        headers = {"auth-token": TestZf.token}
        res = requests.get(url=url, headers=headers)
        print("购物车列表:", res.json())
        TestZf.gid=res.json()['data']['list'][0]["id"]
        print(TestZf.gid)
        assert res.status_code == 200
        assert "ok" in res.text
    def test_qrdd(self):
        url=f"http://10.59.9.12:7001/api/order/confirm?id={TestZf.gid}&type=cart&shipping_type=1"
        headers = {"auth-token": TestZf.token}
        res = requests.get(url=url, headers=headers)
        print("确认订单:", res.json())
        assert res.status_code == 200
        assert "ok" in res.text
    # @pytest.mark.parametrize("data",readjson())
    def test_zf(self):
        conn = pymysql.connect(user="root", password="yidong666", database="a3mall",host="10.59.9.12")
        cur = conn.cursor()
        cur.execute("select id from mall_order")
        sj = cur.fetchall()
        print(sj)
        lastoid=sj[-1][0]


        url="http://10.59.9.12:7001/api/order/create"
        headers = {"content-type": "application/json", "auth-token": TestZf.token}
        data = {"id":TestZf.gid,"type":"cart","address_id":15,"store_id":1,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=44&type=cart"}
        res = requests.post(url=url, headers=headers, json=data)
        print("支付成功:", res.json())
        assert lastoid+1==res.json()['data']['order_id']
        # assert res.status_code == 200
        # assert "ok" in res.text



